﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Rotempco.Core.ORM.Objects;
using Rotempco.Website.Logic;
using System.Collections.Specialized;
using System.Web.UI.HtmlControls;
using Rotempco.Core.Web.Logic;
using Rotempco.Core.Logic;
using Rotempco.Core.Web.CRUD.Objects;

namespace Rotempco.Website.MasterPages.UserControls
{
    public partial class PagingPages : System.Web.UI.UserControl
    {
        public PagingContainer PagingContainer { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                SetPagingPagesPanel();
        }


        private void SetPagingPagesPanel()
        {
            int maxNumberOfPages = 20;

            if (PagingContainer == null)
            {
                placeHolder_PagingPages.Visible = false;
                return;
            }

            int currentPage = GetCurrentPage();

            int numberOfPages = (int)Math.Ceiling((double)PagingContainer.NumberOfResults / (double)PagingContainer.PageSize);
            int showNumberOfPages = Math.Min(numberOfPages, maxNumberOfPages);

            if (numberOfPages < 2)
            {
                placeHolder_PagingPages.Visible = false;
                return;
            }

            literal_NumberOfPages.Text = numberOfPages.ToString();
            literal_CurrentPage.Text = currentPage.ToString();

            List<PagingListItem> pagesList = new List<PagingListItem>();

            pagesList.Add(new PagingListItem()
            {
                Text = "<<",
                PageNumber = (currentPage == 1 ? (int?)null : 1)
            });

            pagesList.Add(new PagingListItem()
            {
                Text = "<",
                PageNumber = (currentPage - 1 > 0 ? currentPage - 1 : (int?)null)
            });

            for (int i = 1; i <= showNumberOfPages; i++)
            {
                pagesList.Add(new PagingListItem()
                {
                    PageNumber = i,
                    Text = i.ToString()
                });
            }

            if (showNumberOfPages < numberOfPages)
            {
                pagesList.Add(new PagingListItem()
                {
                    Text = "...",
                    PageNumber = null
                });
                pagesList.Add(new PagingListItem()
                {
                    Text = numberOfPages.ToString(),
                    PageNumber = numberOfPages
                });
            }

            pagesList.Add(new PagingListItem()
            {
                Text = ">",
                PageNumber = (currentPage + 1 > numberOfPages ? (int?)null : currentPage + 1)
            });

            pagesList.Add(new PagingListItem()
            {
                Text = ">>",
                PageNumber = (currentPage == numberOfPages || currentPage + 1 > numberOfPages ? (int?)null : numberOfPages)
            });

            repeater_PagingPages.DataSource = pagesList;
            repeater_PagingPages.DataBind();
        }

        private int GetCurrentPage()
        {
            int currentPage;
            if (string.IsNullOrWhiteSpace(Request["PageNumber"]) || !int.TryParse(Request["PageNumber"], out currentPage))
                currentPage = 1;
            return currentPage;
        }

        protected void repeater_Paging_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                PagingListItem pagingListItem = e.Item.DataItem as PagingListItem;

                if (pagingListItem.PageNumber.HasValue)
                {
                    if (GetCurrentPage() == pagingListItem.PageNumber.Value)
                    {
                        ((HtmlTableCell)e.Item.FindControl("td_Name")).Style.Add("border", "1px solid lightgray;");
                        ((HtmlTableCell)e.Item.FindControl("td_Name")).Style.Add("background-color", "#b1c4c7;");
                        ((HtmlTableCell)e.Item.FindControl("td_Name")).Style.Add("cursor", "default;");
                    }
                    else
                    {
                        ((HtmlTableCell)e.Item.FindControl("td_Name")).Attributes.Add("onclick", "window.location.href='" + GetPageingUrl(pagingListItem.PageNumber.Value) + "'");
                        ((HtmlTableCell)e.Item.FindControl("td_Name")).Attributes.Add("onmouseover", "style.backgroundColor='lightgray';");
                        ((HtmlTableCell)e.Item.FindControl("td_Name")).Attributes.Add("onmouseout", "style.backgroundColor='transparent';");
                        ((HtmlTableCell)e.Item.FindControl("td_Name")).Style.Add("border", "1px solid lightgray;");
                        ((HtmlTableCell)e.Item.FindControl("td_Name")).Style.Add("cursor", "pointer;");

                    }
                }
                else
                {
                    ((HtmlTableCell)e.Item.FindControl("td_Name")).Style.Add("border", "1px solid #e5e5e5;");
                    ((HtmlTableCell)e.Item.FindControl("td_Name")).Style.Add("color", "#a0a0a0;");
                    ((HtmlTableCell)e.Item.FindControl("td_Name")).Style.Add("cursor", "default;");
                }
            }
        }


        private string GetPageingUrl(int pageNumber)
        {
            NameValueCollection query = HttpUtility.ParseQueryString(Request.Url.Query);

            query["PageNumber"] = pageNumber.ToString();

            return StringLogic.ReplaceString(Request.Url.AbsolutePath, "/WebPages", "", StringComparison.InvariantCultureIgnoreCase) + "?" + query;
        }

    }
}